package com.itheima.admin.controller;

import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "频道管理")
@RestController
@RequestMapping("/channel")
public class AdchannelController {
    @Autowired
    private AdChannelService adChannelService;

    /**
     * 频道列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public PageResultVo pageResult(@RequestBody ChannelPageRequestDto dto){
        PageResultVo vo = adChannelService.findPage(dto);
        return vo;
    }

    @PostMapping("/save")
    @ApiOperation("新增频道")
    public ResultVo add(@RequestBody @Validated AdChannel adChannel){
        adChannelService.add(adChannel);
        return ResultVo.ok();
    }

    /**
     * 通过id查询数据
     * @param channelId
     * @return
     */
    @GetMapping("/{channelId}")
    @ApiOperation("通过id查询")
    public ResultVo findById(@PathVariable Integer channelId){
        AdChannel adChannel = adChannelService.getById(channelId);
        return ResultVo.ok(adChannel);
    }
    @PostMapping("update")
    @ApiOperation("通过id更新")
    public ResultVo update(@RequestBody @Validated AdChannel adChannel){
        adChannelService.updateById(adChannel);
        return ResultVo.ok();
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/{adChannelId}")
    public ResultVo deleteById(@PathVariable Integer adChannelId){
        adChannelService.removeById(adChannelId);
        return ResultVo.ok();
    }
}
